// pages/Details/Details.js
const http = require('../../utils/http.js')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo: {},
    detail: [],
    // 图片地址前置
    baseImg: 'https://dianjiu.oss-cn-beijing.aliyuncs.com',
  },
  tels() {
    wx.makePhoneCall({
      phoneNumber: '15387615114',
    })
  },
  // 跳转到购物车
  toCart() {
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },
  // 跳转到我的
  toMine() {
    wx.switchTab({
      url: '/pages/mine/mine',
    })
  },
  // 添加到购物车
  addToCart() {
    let info = this.data.goodsInfo
    var appInst = getApp()
    let carGoodsArr = appInst.globalData.cartGoods
    let bool = carGoodsArr.some((i) => {
      return i.commodity_id === info.commodity_id
    })
    if (bool) return
    info.num = 1
    info.checked = false
    appInst.globalData.cartGoods.push(info)
    try {
      wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    } catch (e) {}
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel()
    // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', ({ id }) => {
      http({
        url: '/getOneWine?id=' + id,
      }).then((res) => {
        this.setData({
          detail: res,
          goodsInfo: res,
        })
      })
    })
  },
})
